package com.server.llh.llhserverwork.activity.query;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.DialogInterface;


import android.os.Build;
import android.os.Bundle;

import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.family.llh.lelaohuitoastview.dialog.toast.T;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lelaohui.net.BaseLeLaohuiAcitivity;
import com.lelaohui.net.listener.NetDataListener;
import com.lelaohui.net.netmanager.INetRequestManager;
import com.lelaohui.net.netmanager.NetRequestManager;
import com.lelaohui.net.request.LRequest;
import com.lelaohui.net.utils.UrlsTools;
import com.llh.llhsuppliers.R;
import com.server.llh.llhserverwork.adapter.AccountListAdapter;
import com.server.llh.llhserverwork.bean.AccountLiuShuiInfo;
import com.server.llh.llhserverwork.bean.Constants;
import com.server.llh.llhserverwork.utils.ServerPort;
import com.server.llh.llhserverwork.utils.SharedPreferenceUtil;
import com.server.llh.llhserverwork.view.widget.JudgeDate;
import com.server.llh.llhserverwork.view.widget.ScreenInfo;
import com.server.llh.llhserverwork.view.widget.WheelMain;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by ZTF on 2017-07-15.
 */

public class QueryAccoutInfoActivity extends BaseLeLaohuiAcitivity implements View.OnClickListener {

    private ListView health_lv;
    private String userId, userNameStr, token,orgIds;
    private SwipeRefreshLayout refehsLayout;
    private TextView titleName, startTime, stopTime, queryServer;
    private ImageButton main_back;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

        token = SharedPreferenceUtil.getUserInfo(Constants.UserInfo.TOKEN_STR, getApplicationContext());
        orgIds = SharedPreferenceUtil.getUserInfo(Constants.UserInfo.ORG_ID, getApplicationContext());
        startTime = (TextView) findViewById(R.id.startTime);
        startTime.setOnClickListener(this);
        stopTime = (TextView) findViewById(R.id.stopTime);
        stopTime.setOnClickListener(this);
        queryServer = (TextView) findViewById(R.id.queryServer);
        queryServer.setOnClickListener(this);
        titleName = (TextView) findViewById(R.id.titleName);
        titleName.setText("待结账户");
        main_back = (ImageButton) findViewById(R.id.main_back);
        main_back.setOnClickListener(this);
        health_lv = (ListView) findViewById(R.id.health_lv);
        setTimeView(startTime);
        setTimeView(stopTime);
        String startTimeStr = startTime.getText().toString();
        String stopTimeStr = stopTime.getText().toString();
        reqAccount(startTimeStr, stopTimeStr);
    }


    private String monthStr, dayStr;

    public void setTimeView(final TextView view) {
        Calendar calendar = Calendar.getInstance();
        int monthInt = calendar.get(Calendar.MONTH) + 1;
        int dayInt = calendar.get(Calendar.DAY_OF_MONTH);
        if (monthInt < 10) {
            monthStr = String.valueOf("0" + monthInt);
        } else {
            monthStr = String.valueOf(monthInt);
        }
        if (dayInt < 10) {
            dayStr = String.valueOf("0" + dayInt);
        } else {
            dayStr = String.valueOf(dayInt);
        }
        view.setText(calendar.get(Calendar.YEAR) + "-" + (monthStr) + "-"
                + dayStr + "");
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.startTime) {
            showTime(startTime);
            return;
        } else if (v.getId() == R.id.stopTime) {
            showTime(stopTime);
            return;
        } else if (v.getId() == R.id.queryServer) {

            String startTimeStr = startTime.getText().toString();
            String stopTimeStr = stopTime.getText().toString();
            reqAccount(startTimeStr, stopTimeStr);
            return;
        } else {
            onBackPressed();
        }

    }

    AccountListAdapter accountListAdapter = null;
    List<AccountLiuShuiInfo> accountLiuShuiInfos = new ArrayList<>();

    private void reqAccount(String startTime, String endTime) {
        LRequest lRequest = new LRequest();
        lRequest.addParam("sdate", startTime);
        lRequest.addParam("edate", endTime);
        lRequest.addParam("token", token);
        lRequest.addParam("orgIds", orgIds);
        lRequest.setUri(ServerPort.ACCOUNT);
        lRequest.setUriType(UrlsTools.ACCOUNT_INFO);
        lRequest.setReqType("");
        lRequest.setType(String.class);
        requestData(lRequest, new NetDataListener<String>() {
            @Override
            public void onError(Throwable t) {
                T.showAnimErrorToast(getApplicationContext(), "服务器繁忙!");
            }

            @Override
            public void onNext(String responseBean, String code) {
                if (responseBean != null && "0".equals(code)) {
                    accountLiuShuiInfos = new Gson().fromJson(responseBean, new TypeToken<List<AccountLiuShuiInfo>>() {
                    }.getType());
                    if (accountLiuShuiInfos != null && accountLiuShuiInfos.size() > 0) {
                        accountListAdapter = new AccountListAdapter(getApplicationContext(), accountLiuShuiInfos);
                        health_lv.setAdapter(accountListAdapter);
                        accountListAdapter.notifyDataSetChanged();
                    }
                }
            }
        });
    }

    WheelMain wheelMain;
    @SuppressLint("SimpleDateFormat")
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

    public void showTime(final TextView view) {
        LayoutInflater inflater = LayoutInflater
                .from(QueryAccoutInfoActivity.this);
        final View timepickerview = inflater.inflate(R.layout.timepicker, null);
        ScreenInfo screenInfo = new ScreenInfo(QueryAccoutInfoActivity.this);
        wheelMain = new WheelMain(timepickerview);
        wheelMain.screenheight = screenInfo.getHeight();
        String time = view.getText().toString();
        Calendar calendar = Calendar.getInstance();
        if (JudgeDate.isDate(time, "yyyy-MM-dd")) {
            try {
                calendar.setTime(dateFormat.parse(time));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        wheelMain.initDateTimePicker(year, month, day);
        new AlertDialog.Builder(QueryAccoutInfoActivity.this).setTitle("选择时间")
                .setView(timepickerview)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        view.setText(wheelMain.getTime());
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                }).show();

    }

    @Override
    public void showProgress() {
        try {
            showProgress(QueryAccoutInfoActivity.this);
        } catch (Exception e) {

        }
    }

    @Override
    public void hideProgress() {
        try {
            closeProgress();
        } catch (Exception e) {

        }
    }

    @Override
    protected int getlayout() {
        return R.layout.activity_health_task_all;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void doBasess() {

    }

    @Override
    protected INetRequestManager getRequestManager() {
        return NetRequestManager.getInstance();
    }


    @OnClick(R.id.main_back)
    public void onViewClicked() {
        onBackPressed();
    }
}
